package com.czq.gulimail.gulimailproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.common.constant.product.AttrType;
import com.czq.common.utils.PageUtils;
import com.czq.common.utils.Query;
import com.czq.gulimail.gulimailproduct.dao.AttrAttrgroupRelationDao;
import com.czq.gulimail.gulimailproduct.dao.AttrDao;
import com.czq.gulimail.gulimailproduct.entity.AttrAttrgroupRelationEntity;
import com.czq.gulimail.gulimailproduct.entity.AttrEntity;
import com.czq.gulimail.gulimailproduct.service.AttrAttrgroupRelationService;
import com.czq.gulimail.gulimailproduct.service.AttrGroupService;
import com.czq.gulimail.gulimailproduct.service.AttrService;
import com.czq.gulimail.gulimailproduct.service.CategoryService;
import com.czq.gulimail.gulimailproduct.vo.AttrEntityVO;
import com.czq.gulimail.gulimailproduct.vo.resp.AttrResp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrGroupService attrGroupService;


    @Override
    public PageUtils queryPage(Map<String, Object> params, String queryType) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        if (queryType.equals("sale")) {
            queryWrapper.eq("attr_type", AttrType.PRODUCT_ATTR_SALE.getCode());
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateSearchType(Long attrId, Integer searchType) {
        UpdateWrapper<AttrEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("attr_id", attrId).set("search_type", searchType);
        this.update(updateWrapper);
    }

    @Override
    public void updateShowDesc(Long attrId, Integer showDesc) {
        UpdateWrapper<AttrEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("attr_id", attrId).set("show_desc", showDesc);
        this.update(updateWrapper);
    }

    @Override
    public void updateEnable(Long attrId, Integer enable) {
        UpdateWrapper<AttrEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("attr_id", attrId).set("enable", enable);
        this.update(updateWrapper);
    }

    @Override
    public void updateValueType(Long attrId, Integer valueType) {
        UpdateWrapper<AttrEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("attr_id", attrId).set("value_type", valueType);
        this.update(updateWrapper);
    }

    @Transactional
    @Override
    public void saveAttrEntityVO(AttrEntityVO attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.save(attrEntity);

        if (attr.getAttrType().equals(AttrType.PRODUCT_ATTR_BASE.getCode()) && attr.getAttrGroupType() != null) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attr.getAttrGroupType());
            attrAttrgroupRelationService.save(relationEntity);
        }
    }

    @Override
    public AttrResp getAttrRespById(Long attrId) {
        AttrEntity attrEntity = this.getById(attrId);
        AttrResp attrResp = new AttrResp();
        if (attrEntity != null) {
            BeanUtils.copyProperties(attrEntity, attrResp);

            //获取属性绑定的属性分组id
            Long attrId1 = attrEntity.getAttrId();
            QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_id", attrId1);
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao.selectOne(queryWrapper);
            if (attrAttrgroupRelationEntity != null) {
                attrResp.setAttrGroupType(attrAttrgroupRelationEntity.getAttrGroupId());
            }

            //获取该属性所在分类路径
            Long catelogId = attrEntity.getCatelogId();
            Long[] cateIdValues = categoryService.getCatePath(catelogId);
            attrResp.setCateIdValues(cateIdValues);

            Map<String, Object> params = new HashMap<>();
            params.put("page", "1");
            params.put("limit", "10");
            params.put("key", "");

            PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);
            attrResp.setPageUtils(pageUtils);


        }


        return attrResp;
    }

    @Transactional
    @Override
    public void updateAttrEntityVO(AttrEntityVO attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr, attrEntity);
        this.updateById(attrEntity);

        if (attr.getAttrType().equals(AttrType.PRODUCT_ATTR_BASE.getCode())) {
            //先删除原来的关系表
            Long attrId = attrEntity.getAttrId();
            attrAttrgroupRelationService.remove(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));

            //插入新的关系
            Long attrGroupId = attr.getAttrGroupType();
            if (attrGroupId != null) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrId(attrId);
                attrAttrgroupRelationEntity.setAttrGroupId(attrGroupId);
                attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
            }
        }


    }

    @Override
    public void removeAttrAndRelation(List<Long> list) {
        this.removeByIds(list);

        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("attr_id", list);
        attrAttrgroupRelationService.remove(queryWrapper);


    }

    /**
     * 获取分类下的销售属性
     * {
     * page: 1,//当前页码
     * limit: 10,//每页记录数
     * sidx: 'id',//排序字段
     * order: 'asc/desc',//排序方式
     * key: '华为'//检索关键字
     * }
     *
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils querySalePage(Map<String, Object> parmas, Long catelogId) {
        System.out.println(parmas);
        System.out.println(catelogId);
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("catelog_id", catelogId).eq("attr_type", AttrType.PRODUCT_ATTR_SALE.getCode());
        String key = parmas.get("key") != null ? parmas.get("key").toString() : "";
        if (!key.isEmpty())
            queryWrapper.like("attr_name", key).or().like("value_select", key);


        String sidx = parmas.get("sidx") != null ? parmas.get("sidx").toString() : "";
        boolean isAsc = (parmas.get("order") != null ? parmas.get("order").toString() : "").equals("asc");

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(parmas, sidx, isAsc),
                queryWrapper);

        return new PageUtils(page);
    }

}
